﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using KC.DataAccess.MV6;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
		DateTime min = DateTime.Parse("01/01/2005"), max = DateTime.Parse("01/01/2050");
		try {
			if (Request.QueryString["MinArch"] != null) min = DateTime.Parse(Request.QueryString["MinArch"]);
			if (Request.QueryString["min"] != null) min = DateTime.Parse(Request.QueryString["min"]);
			if (Request.QueryString["MaxArch"] != null) max = DateTime.Parse(Request.QueryString["MaxArch"]);
			if (Request.QueryString["max"] != null) max = DateTime.Parse(Request.QueryString["max"]); }
		catch (Exception ex) {
			KC.Logging.Logger.WriteMV6(ex.Message, ex.ToString(), Master.act.ID);
			Response.Redirect("."); }

		if (min != DateTime.Parse("01/01/2005") || max != DateTime.Parse("01/01/2050")) { // show hits for that timespan
			lblSearch.Text = "Displaying all Posts between " + min.ToString("MM/dd/yyyy") + " and " + max.ToString("MM/dd/yyyy");
			using (MV6DataContext dc = new MV6DataContext(ConfigurationManager.ConnectionStrings["MV6"].ConnectionString)) {
				pnlResults.Visible = true;
				var posts = from p in dc.Posts where p.Created > min && p.Created < max orderby p.Created descending select p;
				rResults.DataSource = posts;
				rResults.DataBind(); } }
		else {
			lblSearch.Text = "Displaying all Post quantities grouped by month. Click a month to see a list of Posts from that month.";
			pnlResults.Visible = false; }

		// Show list of months
		using (MV6DataContext dc = new MV6DataContext(ConfigurationManager.ConnectionStrings["MV6"].ConnectionString)) {
			rSearch.DataSource = from p in dc.Posts
								  group p by new { month = p.Created.Month, year = p.Created.Year } into d
								  orderby d.Key.year descending, d.Key.month descending
								  select new { Month = string.Format("{0}/{1}", d.Key.month, d.Key.year), Posts = d.Count() };
			rSearch.DataBind(); } 
	}
	protected void rSearch_ItemDataBound(object sender, RepeaterItemEventArgs e)
	{
		HyperLink hlMonthLink = (HyperLink) e.Item.FindControl("hlMonthLink");
		string moyr = ((dynamic)(e.Item.DataItem)).Month;
		var item = e.Item.DataItem;
		hlMonthLink.Text = moyr;
		DateTime dt = DateTime.Parse(moyr.Split('/')[0] + "/01/" + moyr.Split('/')[1]);
		hlMonthLink.NavigateUrl = "Archives.aspx?MinArch=" + dt.ToString("MM/dd/yyyy") + "&MaxArch=" + dt.AddMonths(1).AddDays(-1).ToString("MM/dd/yyyy");
		

		Label lblNumPosts = (Label)e.Item.FindControl("lblNumPosts");
		lblNumPosts.Text = ((dynamic)(e.Item.DataItem)).Posts.ToString();
	}
	protected void rResults_ItemDataBound(object sender, RepeaterItemEventArgs e)
	{
		if (e.Item == null || e.Item.DataItem == null) return;
		Post p = (Post)e.Item.DataItem;
		
		HyperLink hl = (HyperLink)e.Item.FindControl("hlPost");
		hl.Text = p.Title;
		hl.NavigateUrl = "Post.aspx?Title=" + p.FriendlyName;

		Label lblExcerpt = (Label)e.Item.FindControl("lblExcerpt");
		lblExcerpt.Text = KC.Business.Blog.Search.ExtractExcerpt(new string[0], p.Body);

		Label lblURL = (Label)e.Item.FindControl("lblURL");
		lblURL.Text = hl.NavigateUrl;

		Label lblDate = (Label)e.Item.FindControl("lblDate");
		lblDate.Text = p.Created.ToString();
	}

}